package com.android.jingweiapp.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.XMLPhase.GetVideoDetailXMLPhase;
import com.android.jingweiapp.bean.NewsBean;
import com.android.jingweiapp.util.Contacts;
import com.android.jingweiapp.util.HttpUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.vov.vitamio.Vitamio;


/**
 * 视频新闻
 */
public class NewsDetailVideoActivity extends AppCompatActivity {

    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.tv_detail_title)
    TextView tvDetailTitle;
    @Bind(R.id.tv_detail_time)
    TextView tvDetailTime;
    @Bind(R.id.tv_detail_from)
    TextView tvDetailFrom;
    @Bind(R.id.btn_play)
    Button btn_play;


    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Vitamio.isInitialized(getApplicationContext());
        setContentView(R.layout.activity_info_detail_video);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        progressDialog = ProgressDialog.show(this, "加载中", "请稍后", true, false);
        Parcelable beans = getIntent().getParcelableExtra(Contacts.KEY.KEY_OBJECT);
        getNewsDetail((NewsBean) beans);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    private void dealData(final String str) {
        progressDialog.dismiss();
        btn_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NewsDetailVideoActivity.this, VideoPlayActivity.class)
                        .putExtra(Contacts.KEY.KEY_PATH, Contacts.BASE_PIC_URL + str));
            }
        });
    }

    private void getNewsDetail(NewsBean newsBean) {
        String title = newsBean.getTitle();
        tvDetailTitle.setText(title);
        tvDetailFrom.setText(newsBean.getFrom());
        tvDetailTime.setText(newsBean.getTime());

        new NewsAsyncTask().execute(newsBean.getId());
    }

    class NewsAsyncTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String[] params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    GetVideoDetailXMLPhase.createParam(NewsDetailVideoActivity.this,
                            params[0]),
                    new GetVideoDetailXMLPhase());
        }

        @Override
        protected void onPostExecute(String str) {
            super.onPostExecute(str);
            dealData(str);
        }
    }


}
